#!/usr/bin/env python
# coding: utf-8

import kmodels
import sys

def detect_language(text):
  return 'en'

def load_language_model(text, language):
  l = kmodels.Language(language)
  model = {language:
            {'subject': l.detect_subject(text),
          'object': l.detect_object(text),
          'verb': l.detect_verb(text)}
         }
  model[language]['type'] = _detect_sentence_type(model)
  return model


def _detect_subject(text, language):
  return 'i'


def _detect_object(text, language):
  return 'you'

def _detect_verb(text, language):
  return 'love'

def _detect_sentence_type(language_model):
  """A statement, command, or question."""
  return 'statement'

def translation(text, from_language_model, to_language):

  dictionary = eval(open('en-ak.links.py').read())
  parsed_language_model = {to_language: {'subject': '', 'object': '', 'verb':'', 'type': ''}}
  for language in from_language_model:
    try:
      parsed_language_model[to_language]['subject'] = (
          dictionary['subject'][from_language_model[language]['subject']])
    except KeyError:
      parsed_language_model[to_language]['subject'] = (
          from_language_model[language]['subject'])


    try:
      parsed_language_model[to_language]['object'] = (
          dictionary['object'][from_language_model[language]['object']])
    except:
      parsed_language_model[to_language]['object'] = (
          from_language_model[language]['object'])


    try:
      parsed_language_model[to_language]['verb'] = (
          dictionary['verb'][from_language_model[language]['verb']])
    except:
      parsed_language_model[to_language]['verb'] = (
          from_language_model[language]['verb'])
    #parsed_language_model[to_language]['type'] = from_language_model[language]['subject']['type']
  
  return _generate_sentence(parsed_language_model, to_language)

def _generate_sentence(language_model, language):
  word_order = ['subject', 'verb', 'object']
  sentence = []
  for word_type in word_order:
    sentence.append(language_model[language][word_type].decode('utf-8'))

  return ' '.join(sentence)

def translate(text, to_lang, from_lang=None):
  if not from_lang:
    from_lang = detect_language(text)
  from_lang_model = load_language_model(text, from_lang)
  return translation(text, from_lang_model, to_lang)

def dictionary(language, component):
  l = kmodels.Language(language)
  return l.dictionary[component]

if __name__ == '__main__':
  if len(sys.argv) > 1:
    text = sys.argv[1]
  else:
    text = 'we love Ghana'
  print 'English: %s' % text
  from_lang = detect_language(text)
  print 'Akan: %s' % translate(text, from_lang, 'ak')
